﻿using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.IO;

namespace OpenLS.Spreadsheet.Formats.Biff.Records
{
    internal class FtSbs
    {
        private short _dInc;
        private short _dPage;
        private ushort _dxScroll;
        private bool _fDraw;
        private bool _fDrawSliderOnly;
        private short _fHoriz;
        private bool _fNo3D;
        private bool _fTrackElevator;
        private short _iMax;
        private short _iMin;
        private short _iVal;
        private uint _unused1;

        public void Read(BinaryReader reader)
        {
            ushort ft = reader.ReadUInt16();
            Contract.Assert(ft == 0x000C);
            ushort cb = reader.ReadUInt16();
            Contract.Assert(cb == 0x0014);
            _unused1 = reader.ReadUInt32();
            _iVal = reader.ReadInt16();
            _iMin = reader.ReadInt16();
            _iMax = reader.ReadInt16();
            _dInc = reader.ReadInt16();
            _dPage = reader.ReadInt16();
            _fHoriz = reader.ReadInt16();
            _dxScroll = reader.ReadUInt16();
            ushort ad = reader.ReadUInt16();
            _fDraw = (ad & 0x01) != 0;
            _fDrawSliderOnly = (ad & 0x02) != 0;
            _fTrackElevator = (ad & 0x04) != 0;
            _fNo3D = (ad & 0x08) != 0;
        }

        public void Write(BinaryWriter writer)
        {
            ushort ft = 0x000C;
            writer.Write(ft);
            ushort cb = 0x0014;
            writer.Write(cb);
            writer.Write(_unused1);
            writer.Write(_iVal);
            writer.Write(_iMin);
            writer.Write(_iMax);
            writer.Write(_dInc);
            writer.Write(_dPage);
            writer.Write(_fHoriz);
            writer.Write(_dxScroll);
            ushort ad = 0;
            if (_fDraw) ad |= 0x01;
            if (_fDrawSliderOnly) ad |= 0x02;
            if (_fTrackElevator) ad |= 0x04;
            if (_fNo3D) ad |= 0x08;
            writer.Write(ad);
        }
    }
}